using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Adge.BLL;
using Adge.Models;
using NHibernate.Expression;
using Adge.Common;
using System.Collections.Generic;

namespace Adge.Web.UI
{
    public partial class MoreNews : BasePage
    {
        private Int32 CatId
        {
            get
            {
                Int32 _catId = -1;
                if (Request["catid"] != null)
                {
                    Int32.TryParse(Request["catid"], out _catId);
                }
                return _catId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitCatAndNewsTitle();
            }
        }

        private void InitCatAndNewsTitle()
        {
            //int recordstart = 0;
            
            AdgeArBase<CategoryInfo> bllCat = new AdgeArBase<CategoryInfo>();
            CategoryInfo cat = bllCat.GetEntityById(CatId);

            //NewsInfo[] entities ;
            if (cat != null)
            {
                litTitle.Text = cat.Catname;
                List<NewsInfo> news = new List<NewsInfo>(cat.AdgeNews);
                ListComparer<NewsInfo> newscomp = new ListComparer<NewsInfo>("PubDate");
                news.Sort(newscomp);
                news.Reverse();
                clNews.DataSource = news;
                clNews.DataBind();
            }
        }

        protected IList GetGlobalCategory(int ParentID)
        {
            AdgeArBase<AttachmentInfo> catlist = new Adge.BLL.AdgeArBase<AttachmentInfo>();

            AdgeArBase<CategoryInfo> bllCat = new AdgeArBase<CategoryInfo>();
            Order[] ordersCat = new Order[] { };
            ICriterion[] criteriasCat = new ICriterion[] { new EqExpression("CatId", ParentID) };
            CategoryInfo[] cats = bllCat.SlicedFindAll(0, 2, ordersCat, criteriasCat);

            ICriterion[] criterias = new ICriterion[] { new EqExpression("AdgeInfoCategory", cats[0]) };
            Order[] orders = new Order[] { Order.Asc("AttOrder") };
            AttachmentInfo[] entities = catlist.SlicedFindAll(0, 5, orders, criterias);
            return entities;
        }

        protected void PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            clNews.CurrentPageIndex = e.NewPageIndex;
            clNews.DataBind();
            //clNews.SetPage(e.NewPageIndex);
        }
    }
}
